package com.ubi.core.http;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

/**
 * 标题、简要说明. <br>
 * 类详细说明.
 * <p>
 * Copyright: Copyright (c) 2014年7月23日 上午10:40:58
 * <p>
 * Company: 网龙公司
 * <p>
 *
 * @author Lijj
 * @version 1.0.0
 */
public class HttpConnectionManager {

    private static final int REQUEST_TIMEOUT = 1000 * 20;

    private static final int SO_TIMEOUT = 1000 * 5;

    private static PoolingClientConnectionManager clientConnectionManager;

    private static HttpClient httpClient;

    private static synchronized ClientConnectionManager getConnectionManager() {

        if (clientConnectionManager == null) {
            clientConnectionManager = new PoolingClientConnectionManager();
        }
        return clientConnectionManager;
    }

    public static synchronized HttpClient getHttpClient() {
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setUseExpectContinue(params, true);

        HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);

        httpClient = new DefaultHttpClient(getConnectionManager(), params);
        return httpClient;
    }

}
